<template>
  <view class="page">
    <view class="search-bar">
      <view class="search-input">
        <input type="text" v-model="keywords" placeholder="请输入标题/短信内容" />
        <image class="icon-search" src="/static/images/icon_search.png" mode=""></image>
        <view class="search-button" @click="search">搜索</view>
      </view>
    </view>
    <view class="tabs">
      <view v-for="(item, index) in statusList" :key="index" class="tab" :class="{active: index === swiperCurrent}"
        @click="change(index)">{{item.label}}</view>
    </view>
    <scroll-view class="scroll-list" scroll-y="true" @scrolltolower="loadMore">
      <view class="message-list">
        <view v-for="(item, index) in list" :key="index" class="message-item" @click="handleClick(item)">
          <view class="message-item-bd">
            <view class="message-title">{{item.name}}</view>
            <view class="message-desc">{{item.createTime}}</view>
            <view class="message-status" :class="{disabled: item.status}">{{item.statusName}}</view>
          </view>
        </view>
      </view>
      <u-loadmore class="loadmore" :status="loadStatus"></u-loadmore>
    </scroll-view>
    <view v-if="hasPermission('msgTemplate_add')" class="add-btn" @click="navTo('form')">
      <image src="/static/images/icon_add_message.png" mode=""></image>
    </view>
  </view>
</template>
<script>
  import $time from '@/common/time.js';
  export default {
    data() {
      return {
        keywords: '',
        status: '',
        query: {
          current: 1,
          size: 10
        },
        list: [],
        pages: 0,
        loadStatus: 'loadmore',
        options: [],
        headStyle: {
          fontWeight: 'bold',
          color: '#606266',
          margin: '16rpx'
        },
        statusList: [],
        swiperCurrent: 0,
      };
    },
    onLoad() {
      this.loadStatusList();
    },
    computed: {

    },
    methods: {
      navTo(url) {
        uni.navigateTo({
          url: url
        });
      },
      search(value) {
        this.list = [];
        this.query.current = 0;
        this.loadList();
      },
      loadList() {
        this.query.keywords = this.keywords;
        this.query.status = this.statusList[this.swiperCurrent].value;
        this.$u.api.message.msgTemplate.list(this.query).then(res => {
          if (!res.data.records || res.data.records.length == 0) {
            this.loadStatus = "nomore";
            return;
          }
          res.data.records.forEach((item) => {
            item.show = false;
          })
          this.list = this.list.concat(res.data.records);
          this.pages = res.pages;
          this.query.current = res.data.current;
          this.query.size = res.data.size;
          this.loadStatus = "loadmore";
        });
      },
      loadMore() {
        this.loadStatus = "loading";
        setTimeout(() => {
          this.query.current += 1;
          this.loadList();
        }, 100);
      },
      getContent(value) {
        let res = value.replace(/<.+?>/g, '').replace(/&nbsp;/ig, '').replace(/\s/ig, '');
        return res;
      },
      handleDelete(row) {
        let self = this;
        uni.showModal({
          title: '提示',
          content: '确认要删除该数据吗,删除后无法恢复？',
          showCancel: true,
          success: function(res2) {
            if (res2.confirm) {
              self.$u.api.message.msgTemplate.delete({
                ids: row.id
              }).then(res => {
                self.$u.toast('删除成功！');
                self.search();
              });
            }
          }
        });
      },
      change(index) {
        this.list = [];
        this.query.current = 0;
        this.swiperCurrent = index;
        this.loadList();
      },
      loadStatusList() {
        this.$u.api.dictBizData({
          code: 'status_type'
        }).then(res => {
          let list = [];
          let json = {
            'label': '全部',
            'value': ''
          }
          list.push(json)
          res.data.forEach((item) => {
            list.push({
              'label': item.dictValue,
              'value': item.dictKey
            });
          })
          this.statusList = list;
          this.loadList();
        });
      },
      handleClick(row) {
        if (this.hasPermission('msgTemplate_edit') || this.hasPermission('msgTemplate_view')) {
          this.navTo('form?id=' + row.id);
        }
      }
    }
  };
</script>
<style lang="scss">
  .page {
    position: relative;
    background: #F7F7F7;
  }

  .search-bar {
    display: flex;
    align-items: center;
    padding: 16rpx 32rpx;
    background: #fff;
  }

  .search-input {
    position: relative;
    width: 100%;
    height: 64rpx;
    border-radius: 32rpx;
    background: #F7F7F7;
    overflow: hidden;

    input {
      width: 100%;
      height: 64rpx;
      border: 0;
      padding: 0 144rpx 0 64rpx;
      outline: 0;
      line-height: 64rpx;
      font-size: 24rpx;
      font-weight: 500;
      color: #999;
      background: transparent;
    }

    .icon-search {
      position: absolute;
      top: 21rpx;
      left: 21rpx;
      width: 22rpx;
      height: 22rpx;
    }

    .search-button {
      position: absolute;
      top: 0;
      right: 0;
      width: 144rpx;
      border-radius: 32rpx;
      line-height: 64rpx;
      text-align: center;
      font-size: 24rpx;
      font-weight: 700;
      color: #fff;
      background: #3989FC;
    }
  }

  .tabs {
    position: relative;
    display: flex;
    height: 88rpx;
    background: #fff;

    &::before {
      content: '';
      position: absolute;
      top: 0;
      right: 0;
      left: 0;
      display: block;
      border-top: 1px solid #F7F7F7;
    }

    .tab {
      position: relative;
      width: 100%;
      line-height: 88rpx;
      text-align: center;
      font-size: 28rpx;
      font-weight: 500;
      color: #999;

      &::after {
        content: '';
        position: absolute;
        bottom: 4rpx;
        left: 50%;
        display: inline-block;
        width: 32rpx;
        height: 8rpx;
        border-radius: 4rpx;
        background: transparent;
        transform: translateX(-50%);
      }

      &.active {
        font-weight: 700;
        color: #333;

        &::after {
          background: #3989FC;
        }
      }
    }
  }

  .scroll-list {
    height: calc(100vh - var(--window-top) - var(--window-bottom) - 96rpx - 88rpx);

    .loadmore {
      padding: 32rpx;
    }
  }

  .message-list {
    display: flex;
    flex-direction: column;
    padding: 24rpx 24rpx 0;
  }

  .message-item {
    width: 100%;
    border-radius: 8rpx;
    margin-bottom: 24rpx;
    background: #fff;
  }

  .message-item-bd {
    position: relative;
    padding: 32rpx;
  }

  .message-title {
    padding-right: 96rpx;
    margin-bottom: 16rpx;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-weight: 700;
    font-size: 32rpx;
    color: #333;
    overflow: hidden;
  }

  .message-desc {
    font-size: 24rpx;
    font-weight: 500;
    color: #999;
  }

  .message-status {
    position: absolute;
    top: 32rpx;
    right: 32rpx;
    display: inline-block;
    border-radius: 24rpx;
    padding: 0 24rpx;
    line-height: 48rpx;
    text-align: center;
    font-size: 24rpx;
    font-weight: 500;
    color: #3989FC;
    background: #DEEBFE;

    &.disabled {
      color: #FF6600;
      background: rgba(255, 102, 0, .15);
    }
  }

  .add-btn {
    position: absolute;
    right: 32rpx;
    bottom: 288rpx;
    z-index: 99;
    width: 88rpx;
    height: 88rpx;

    image {
      width: 88rpx;
      height: 88rpx;
      vertical-align: top;
    }
  }
</style>